package com.enhinck.dbutilweb.controller;

import com.enhinck.dbutilweb.common.freemark.JavaClassEntity;
import com.enhinck.dbutilweb.config.SqlExecUtil;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 * 描述
 *
 * @author huenbin
 * @date 2020-07-01 11:00
 */
@Component
@Slf4j
public class WebFreemarkUtil {

    static Configuration configuration;

    @PostConstruct
    public void init() {
        configuration = new Configuration(Configuration.VERSION_2_3_28);
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        List<FileEntity> files = SqlExecUtil.scanTemplate("javaTemplate");
        files.forEach(file -> {
            try {
                log.info("{}", file.getFileName());
                String name = file.getFileName();
                String template = file.getFileContent();
                stringTemplateLoader.putTemplate(name, template);
                log.info("putTemplate:{}", name);
            } catch (Exception e) {
                log.error("", e);
            }
        });
        log.info("StringTemplateLoader LOAD SUCCESS");
        configuration.setTemplateLoader(stringTemplateLoader);
    }


    public static void waitM(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void write(JavaClassEntity javaClassEntity, HttpServletResponse response) {
        Writer out = null;
        try {
            String fileName = javaClassEntity.getClassName() + ".java";
            response.reset();
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader(
                    "Content-disposition",
                    "attachment; filename="
                            + new String(fileName.getBytes("utf-8"),
                            "ISO8859-1"));
            out = response.getWriter();
            String packagePath = javaClassEntity.getPackagePath();
            Template template;
            if (javaClassEntity.isInterface()) {
                template = configuration.getTemplate("iservice.ftl");
            } else {
                template = configuration.getTemplate("service.ftl");
            }
            template.process(javaClassEntity, out);
            log.info("『{}』类生成成功", javaClassEntity.getClassName());
        } catch (Exception e) {
            log.info("类生成错误{}", e);
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }


}
